﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NENAData.LINQ;
using NENAData.Email;

namespace  NENAData.NENAMember.Referral
{
    public class Generate
    {

        public void GenerateUnqiueMemberReferralCode(int intMemberId)
        {
            NENAMember oNENAMember = new NENAMember();
            Member oMember = oNENAMember.GetMember(intMemberId);

            if (oMember.Referral_Code == string.Empty)
            {
                string strReferralCode = GetUniqueReferralCode();
                oNENAMember.SetMemberReferralCode(intMemberId, strReferralCode);
                NENAData.Email.Referral.SendMemberReferralEmail(intMemberId, strReferralCode);
            }
            else
                NENAData.Email.Referral.SendMemberReferralEmail(intMemberId, oMember.Referral_Code); 
        }

        private void SaveMemberReferralCode()
        {
        }

        private string  GetUniqueReferralCode()
        {
            Referral oReferral = new Referral();
            Referral_Code oReferral_Code = new Referral_Code();
            string strCode;

            do
            {
                strCode = oReferral.GenerateAReferralCode();
            }
            while (oReferral.IsReferralCodeAlreadyUsed(strCode));

            return strCode.ToUpper();
        }

    }
}
